package dev.langchain4j.example.agent;

import dev.langchain4j.agent.tool.ToolSpecification;
import dev.langchain4j.example.tools.BookingTools;
import dev.langchain4j.example.tools.TASKTYPE;
import dev.langchain4j.memory.chat.ChatMemoryProvider;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.chat.ChatModel;
import dev.langchain4j.model.chat.StreamingChatModel;
import dev.langchain4j.model.chat.request.json.JsonObjectSchema;
import dev.langchain4j.model.openai.OpenAiChatModel;
import dev.langchain4j.rag.content.retriever.ContentRetriever;
import dev.langchain4j.service.AiServices;
import dev.langchain4j.service.SystemMessage;
import dev.langchain4j.service.UserMessage;
import dev.langchain4j.service.tool.ToolExecutor;
import dev.langchain4j.service.tool.ToolProvider;
import dev.langchain4j.service.tool.ToolProviderResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

@Slf4j
@Configuration
public class Agents {


    interface GreetingExpert {

        @UserMessage("判断用户需求，如果需要查询具体： {{it}}")
        TASKTYPE isTask(String text);

    }

    interface ChatBot {

        @SystemMessage("你是一名航空公司客服代理，请为客户服务：")
        String reply(String userMessage);
    }


    @Bean
    public GreetingExpert greetingExpert() {

        ChatModel doubaoLite = OpenAiChatModel.builder()
                .apiKey("26f9ab1f-3509-4f8e-b06e-7495bc93373a")
                .modelName("doubao-lite-4k-character-240828")
                .baseUrl("https://ark.cn-beijing.volces.com/api/v3")
                .build();


        return AiServices.builder(GreetingExpert.class)
                .chatModel(doubaoLite)
                .build();

    }

    @Bean
    public ChatBot chatBot() {

        ChatModel bot = OpenAiChatModel.builder()
                .apiKey("26f9ab1f-3509-4f8e-b06e-7495bc93373a")
                .modelName("bot-20250514134159-b4kxm")
                .baseUrl("https://ark.cn-beijing.volces.com/api/v3/bots")
                .build();


        return AiServices.builder(ChatBot.class)
                .chatModel(bot)
                .build();

    }



    @Bean
    public CustomerSupportAgent footBallAssistant(StreamingChatModel streamingChatModel, BookingTools toolsService, ContentRetriever contentRetriever) {


        ChatMemoryProvider chatMemoryProvider = memoryId -> MessageWindowChatMemory.builder()
                .id(memoryId)
                .maxMessages(10)
                .build();


        ToolProvider toolProvider = (toolProviderRequest) -> {
            if (toolProviderRequest.userMessage().singleText().contains("booking")) {
                ToolSpecification toolSpecification = ToolSpecification.builder()
                        .name("get_booking_details")
                        .description("Returns booking details")
                        .parameters(JsonObjectSchema.builder()
                                .addStringProperty("bookingNumber")
                                .build())
                        .build();
                ToolExecutor toolExecutor = (toolExecutionRequest, memoryId) -> {
                    String arguments = toolExecutionRequest.arguments();
                    String name = toolExecutionRequest.name();
                    //根据模型名称、参数 手动执行
                    log.info("=====================" + name + "工具执行成功，参数：" + arguments);

                    return name + "工具执行成功，参数：" + arguments;
                };

                return ToolProviderResult.builder()
                        .add(toolSpecification, toolExecutor)
                        .build();
            } else {
                return null;
            }
        };


        return AiServices.builder(CustomerSupportAgent.class)
                //.tools(toolsService)
                //.toolProvider(toolProvider)
                .contentRetriever(contentRetriever)
                .streamingChatModel(streamingChatModel)
                .chatMemoryProvider(chatMemoryProvider)
                .build();
    }

}
